$(function(){


	//获取类别下拉列表
	$.ajax({
		url:'./Runtime/Cache_long/indexButtonGroup_tabType?r='+Math.random(),
		async:false,
		success:function(res){
			//$('#indexButtonGroup_tabType').html(res);
			setSelect(res);
		},
		error:function(){
			$.ajax({
				url:'./Control/indexButtonGroup_tabType.php',
				async:false,
				success:function(res){
					//alert(res);
					setSelect(res);
				}
			});
		}
	});
	function setSelect(res){
		var option_tmp = "<select id='indexButtonGroup_select'><option value='all' selected>全部</option>";
		if(res != '[]'){
			var tabType = JSON.parse(res);
			tabType.forEach(function(v,i,t){
				option_tmp += "<option value='"+v.id+"'>"+decodeURIComponent(v.name)+"</option>";
			});
			
		}
		option_tmp += "</select>";
		$('#indexButtonGroup_tabType').html(option_tmp);
	}


	exit;




	if(sessionStorage.loginStat == 'true'){
		$('#login').removeClass('displayBlockInline').addClass('displayNone');
		$('#exit').removeClass('displayNone').addClass('displayBlockInline');
	}else{

		$('#indexButtonGroup_infoAdd').hide();
		$('#indexButtonGroup_infoDel').hide();
		$('#indexButtonGroup_tabTypeAdd').hide();
		$('#indexButtonGroup_tabTypeDel').hide();
		$('#indexButtonGroup_clearCache').hide();
		$('#indexButtonGroup_joinAll').hide();
		$('#login').removeClass('displayNone').addClass('displayBlockInline');
		$('#exit').removeClass('displayBlockInline').addClass('displayNone');
	}

	$('#login').click(function(){
		location.href='login.html';
	});
	$('#exit').click(function(){
		sessionStorage.loginStat = false;
		location.reload();
	});

	$('.clearLeftRightInforandom').click(function(){
		$('#index_left').val('');
		$('#index_right').val('');
		//sessionStorage.infoRandom = '';
	});


	//清除缓存
	$('#indexButtonGroup_clearCache').click(function(){
		localStorage.clear();
		$.ajax({
			url:'./Control/clearCache.php',
			success:function(res){
				alert('缓存以清除成功！');
			}
		});
	});



	//缓存当前选择的类别，并维持状态
	if(sessionStorage.indexButtonGroup_tabType){
		$('#indexButtonGroup_select').val(sessionStorage.indexButtonGroup_tabType);
	}else{
		sessionStorage.indexButtonGroup_tabType = $('#indexButtonGroup_select').find('option:selected').val();
	}
	$('#indexButtonGroup_select').change(function(){
		sessionStorage.randSum = '0';
		$('#index_right,#index_left').val('');
		sessionStorage.indexButtonGroup_tabType = $('#indexButtonGroup_select').find('option:selected').val();
	});


	//总览
	$('#indexButtonGroup_infoAll').click(function(){
		var tid = $('#indexButtonGroup_select').find('option:selected').val();
		var tName = $('#indexButtonGroup_select').find('option:selected').text().trim();
		if(tid == 'all'){ 
			toast('请选择类别！',1000,1);
			return;
		}
		$.ajax({
			url:'./Runtime/CacheInfoAll/'+tName+'?r='+Math.random(),
			success:function(res){
				format_infoAll(res);
				//$('#index_left').val(res);
			},
			error:function(){
				$.ajax({
					url:'./Control/indexButtonGroup_infoAll.php',
					data:{
						tid:tid,
						tName:tName
					},
					type:'post',
					success:function(res){
						format_infoAll(res)
					},
					error:function(){
						alert('ajax is error!');
					}
				});
			}
		});
	});
	function format_infoAll(res){
		var infoAll = JSON.parse(res);
		var tmp_str = '总共'+infoAll.length+"条数据!\r\n";
		infoAll.forEach(function(v,i,t){
			tmp_str += decodeURIComponent(v.lefts)+"\r\n\r\n";
		});
		$('#index_left').val(tmp_str);
	}






	//随机浏览
	$('#indexButtonGroup_infoRandom').click(function(){
		$('#index_right').val('');
		$('#index_left').val('');
		if(sessionStorage.randSum){
			var sum = parseInt(sessionStorage.randSum);
			sum ++;
			sessionStorage.randSum = sum;
			$('#index_right').val('今天共浏览'+sum+'条，');
		}else{
			sessionStorage.randSum = 1;
			$('#index_right').val('今天共浏览1条，');
		}
		var tid = $('#indexButtonGroup_select').find('option:selected').val();
		var tName = $('#indexButtonGroup_select').find('option:selected').text().trim();



		$.ajax({
			url:'./Control/indexButtonGroup_infoRandom.php',
			type:'post',
			async:false,
			data:{
				tid:tid,
				tName:tName
			},
			success:function(res){
				var lefts = decodeURIComponent(res);
				$('#index_left').val(lefts);	
			}
		});
	
	});



	//模糊查询
	$('#indexButtonGroup_infosQuery,#indexButtonGroup_infosQuery2').click(function(){
		$('#index_right').val('');
		var left = encodeURIComponent($('#index_left').val().trim());
		var tid = $('#indexButtonGroup_select').find('option:selected').val();
		var tName = $('#indexButtonGroup_select').find('option:selected').text().trim();

		if(tid == 'all'){
			alert('请选择相应的类别，再模糊查询！');return;
			tName = 'all';
		}
		if(!left){
			return;
		}
		if($(this).attr('id') == 'indexButtonGroup_infosQuery'){
			var search_type = 'lefts';
			var fileName = './Runtime/CacheLeftVague/'+tName+'/'+md5(left)+"?r="+Math.random();
		}else{
			var search_type = 'rights';
			var fileName = './Runtime/CacheRightVague/'+tName+'/'+md5(left)+"?r="+Math.random();
		}
		$.ajax({
			url:fileName,
			success:function(res){
				format_infosQuery(res);
			},
			error:function(){
				$.ajax({
					url:'./Control/indexButtonGroup_infosQuery.php',
					type:'post',
					data:{
						tid:tid,
						tName:tName,
						left:left,
						search_type:search_type
					},
					success:function(res){
						if(res == 'err'){
							$('#index_right').val('查询的数据不存在 !');
						}else{
							format_infosQuery(res);
							//$('#index_right').val(res);
						}
					},
					error:function(){
						alert('ajax is bad !');
					}
				});
			}
		});
	});
	function format_infosQuery(res){
		var tmp_str = '';
		JSON.parse(res).forEach(function(v,i,t){
			tmp_str += decodeURIComponent(v.lefts)+"\r\n--->\r\n"+decodeURIComponent(v.rights)+"\r\n------------\r\n\r\n\r\n";
		});
		$('#index_right').val(tmp_str);
	}


	//查询
	$('#indexButtonGroup_infoQuery').click(function(){
		$('#index_right').val('');
		var tid = $('#indexButtonGroup_tabType').find('option:selected').val();
		var tName = $('#indexButtonGroup_tabType').find('option:selected').text().trim();
		var left = encodeURIComponent($('#index_left').val().trim());
		if(!left){
			return;
		}
		//alert(md5(left+tid));
		if(tid == 'all'){
			tName = 'all';
		}
		var fileName = './Runtime/CacheRight/'+tName+'/'+md5(left)+'?r='+Math.random();
		$.ajax({
			url:fileName,
			success:function(res){
				data_format_infoQuery(res);
			},
			error:function(){
				$.ajax({
					url:'./Control/indexButtonGroup_infoQuery.php',
					type:'post',
					data:{
						tid:tid,
						tName:tName,
						left:left,
					},
					success:function(res){
						if(res == 'err'){
							$('#index_right').val('没有找到对应的数据！');
						}else{
							data_format_infoQuery(res);
						}
					}
				});
			}
		});
	});
	function data_format_infoQuery(res){
		var arr = JSON.parse(res);
		var tmp_right = '';

		if(arr.length > 1){
			arr.forEach(function(v,i,t){
				tmp_right += decodeURIComponent(v)+"\r\n-------\r\n\r\n";
			});							
		}else{
			tmp_right = decodeURIComponent(arr[0]);
		}
		$('#index_right').val(tmp_right);
	}








	//添加类别
	$('#indexButtonGroup_tabTypeAdd').click(function(){
		$('#indexButtonGroup_tabTypeGroup').removeClass('displayNone');
	});
	$("#indexButtonGroup_tabTypeAddSub").click(function(){
		var tabName = encodeURIComponent($('#indexButtonGroup_tabTypeAddText').val().trim());
		if(tabName.length <1 || tabName.length > 100){
			alert('类别名不能为空或过长！');
			return;
		}
		var val = setToken('add_tabtype');
		$.ajax({
			url:"./Control/indexButtonGroup_tabTypeAdd.php",
			data:{
				tabName:tabName,
				token:val
			},
			type:'post',
			success:function(res){
				if(res == 'ok'){
					alert('添加成功！');
					location.reload(1);
				}else{
					alert(res);
				}
			}
		});
	});

	//删除类别
	$('#indexButtonGroup_tabTypeDel').click(function(){
		
		var tName = $("#indexButtonGroup_select").find('option:selected').text().trim();
		var tid = $("#indexButtonGroup_select").find('option:selected').val().trim();
		if( tName == '全部' || !confirm('确认删除"'+tName+'"及其数据？不可恢复！')){
			return;
		}
		if(prompt('输入密码！') != 'xiaodong123'){
			return;
		}
		var token = setToken('tabTypeDel');

		$.ajax({
			url:'./Control/indexButtonGroup_tabTypeDel.php',
			type:'post',
			data:{
				token:token,
				tid:tid,
				tName:tName
			},
			success:function(res){
				alert(res);
				location.reload(1);
				sessionStorage.indexButtonGroup_tabType = '';	//清除类别id缓存
			}
		});
	});




	//添加信息
	$('#indexButtonGroup_infoAdd').click(function(){
		var tid = $('#indexButtonGroup_select').find('option:selected').val();
		var tName = $('#indexButtonGroup_select').find('option:selected').text().trim();
		if(tid == 'all'){alert('请选择类别！');return;}
		var data = $('#index_left').val();

		var data2 = data.split('--->');
		if(!data2[0] || !data2[1] ){
			alert('添加数据格式为: a ---> aaa');
			return;
		}
		var token = setToken('infoAdd');
		var left = encodeURIComponent(data2[0].trim());
		var right = encodeURIComponent(data2[1].trim());
		$.ajax({
			url:'./Control/indexButtonGroup_infoAdd.php',
			data:{
				left:left,
				right:right,
				tid:tid,
				tName:tName,
				token:token
			},
			type:'post',
			success:function(res){
				toast(res,2000,1);
			},
			error:function(){
				toast('ajax is error',1000,1);
			}
		});

	});


	//数据删除
	$('#indexButtonGroup_infoDel').click(function(){
		var tmpVal = $('#index_left').val().trim();
		var tid = $('#indexButtonGroup_select').find('option:selected').val();
		var tName = $('#indexButtonGroup_select').find('option:selected').text().trim();
		if(tid == 'all'){
			alert('全部状态下不可删除！');return;
		}
		if(!tmpVal){
			alert('请输入要删除的信息！');return;
		}
		if(!confirm('确认删除数据"'+tmpVal+'"?')){
			return;
		}
		var token = setToken('infoDel');
		var index_left = encodeURIComponent($('#index_left').val().trim());
		$.ajax({
			url:'./Control/indexButtonGroup_infoDel.php',
			data:{
				token:token,
				left:index_left,
				tid:tid,
				tName:tName
			},
			type:'post',
			success:function(res){
				alert(res);
			},
			error:function(){
				alert('ajax is error!');
			}
		});
	});


	//是否参与All浏览
	$('#indexButtonGroup_joinAll').click(function(){
		if(!confirm('确认要操作?')){
			return;
		}
		var selectVal = $('#index_button_groups').find('option:selected').val();
		if(selectVal == 'all'){
			alert('全部选项时不可用！');return;
		}
		$.ajax({
			url:'./Control/indexButtonGroup_joinAll.php',
			data:{
				tid:selectVal
			},
			type:'post',
			success:function(res){
				alert(res);
				location.reload(1);
			}
		});
	});
});